package com.spring.dynamic.jar.core;

import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.Set;

/**
 * AddonBeanDefinitionScanner
 *
 * @author Brant Liu <br> 邮箱：<br>lbf1988@qq.com <br>日期：<br>2018/04/09
 * @version 1.0.0
 */
public class AddonScanner extends ClassPathBeanDefinitionScanner {
    public AddonScanner(BeanDefinitionRegistry registry) {
        super(registry);
    }

    @Override
    protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
        if(beanDefinitions.isEmpty()){
            logger.warn("No Addon bean was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");
        }else{
            for (BeanDefinitionHolder holder : beanDefinitions) {
                GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
                ScannedGenericBeanDefinition beanHolder = (ScannedGenericBeanDefinition) holder.getBeanDefinition();
                if(beanHolder.getMetadata().hasAnnotation(Controller.class.getName())){
                    ContextHolder.getBean(MappingHandler.class).detectHandlerMethods(holder.getBeanName());
                }
            }
        }
        return beanDefinitions;
    }
}
